/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.front.pickup;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.services.front.entrepot.EntrepotService;
import shop.services.front.entrepot.bean.Entrepot;
import shop.services.front.pickup.PickupService;
import shop.services.front.pickup.bean.Pickup;
import shop.services.manage.system.bean.User;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @类名称：PickupController
 * @创建人：Wzl
 * @创建时间：2017-10-24 上午10:00:07
 * @版本号：1.0
 * @描述：分销提货
 */
@Controller
@RequestMapping("/front/pickup/")
public class PickupController extends FrontBaseController<Pickup> {
    @Resource
    private PickupService pickupService;
    @Resource
    private EntrepotService entrepotService;
    @Override
    public Services<Pickup> getService() {
        return pickupService;
    }

    /**
     * 跳转到提货页面
     *
     * @return
     */
    @RequestMapping("toPickupAdd")
    public String toPickupAdd() {
        return "/front/depot/pickGoods";
    }

    /**
     * 跳转到提货详情页面
     *
     * @return
     */
    @RequestMapping("toPickupDetail")
    public String toPickupDetail() {
        return "/front/depot/pickupDetail";
    }


    /**
     * 查询分销提货列表
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("selectPickupList")
    @ResponseBody
    public JSONResult selectPickupList(HttpSession session,Pickup pickup) throws Exception {
        User user = (User) session.getAttribute("userInfo");
        pickup.setUserID(user.getId());
        List<Pickup> rsList = pickupService.selectPickupList(pickup);
        logger.debug("查询分销提货列表："+rsList);
        jsonResult = new JSONResult();
        jsonResult.setData(rsList);
        return jsonResult;
    }

    /**
     * 查询分销提货详情
     * @param pickup
     * @return
     */
    @RequestMapping(value = "selectPickupDetail", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectPickupDetail(Pickup pickup) {
        pickup =  pickupService.selectPickupDetail(pickup);
        jsonResult = new JSONResult();
        jsonResult.setData(pickup);
        return jsonResult;
    }

    /**
     * 分销商提货
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("insertPickup")
    @ResponseBody
    public JSONResult insertPickup(HttpSession session,Pickup pickup) throws Exception {
        jsonResult = new JSONResult();
        User user = (User) session.getAttribute("userInfo");
        Entrepot entrepot =new Entrepot();
        entrepot.setId(pickup.getEntrepotID());
        entrepot=entrepotService.selectOne(entrepot);
        if(entrepot.getNumber()- pickup.getNumber() >=0){
            pickup.setUserID(user.getId());
            pickup.setIsSent("1");
            pickupService.insert(pickup);
            entrepot.setNumber(entrepot.getNumber()-pickup.getNumber());
            entrepotService.update(entrepot);
            return jsonResult;
        }else{
            jsonResult.setMessage("您的库存不足，无法取货！");
            jsonResult.setSuccess(false);
            return jsonResult;
        }
    }

}
